package com.kiln.juxingqs.ui

import android.content.Intent
import android.view.View
import client.global.commlib.toast.toast
import client.global.commlib.utils.StoreUtil
import com.blankj.utilcode.util.LogUtils
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.api.bean.UserInfo
import com.kiln.juxingqs.databinding.ActivityCreaterechargeBinding
import com.kiln.juxingqs.dialog.PayDetailDialog
import com.kiln.juxingqs.ui.vm.CreateRechargeViewModel


class CreateRechargeActivity :
    BaseMiddleActivity<CreateRechargeViewModel, ActivityCreaterechargeBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_createrecharge
    }

    override fun initViewModel(): CreateRechargeViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
    }

    override fun initData() {
//        viewModel.callUserInfo()

        StoreUtil.getBean<UserInfo>(this@CreateRechargeActivity)?.let {
            binding.typeOne.visibility =
                if (it.pay_status.contains("1")) View.VISIBLE else View.GONE
            binding.typeTwo.visibility =
                if (it.pay_status.contains("2")) View.VISIBLE else View.GONE
            binding.typeThree.visibility =
                if (it.pay_status.contains("3")) View.VISIBLE else View.GONE
            binding.typeFour.visibility =
                if (it.pay_status.contains("4")) View.VISIBLE else View.GONE
            binding.typeFive.visibility =
                if (it.pay_status.contains("5")) View.VISIBLE else View.GONE
        }
    }

    override fun observerUI() {
        viewModel.okCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    if (viewModel.txType.value == 1) {
                        startActivity(Intent(this, RechargeActivity::class.java))

                    } else if (viewModel.txType.value == 3) {
                        //新充值银行卡 方式 w_type =3
                        if (it.data?.payment_id.isNullOrEmpty()) {
                            toast("参数空")
                            return@let
                        }
                        it.data?.payment_id?.let {
                            startActivity(Intent(this, Bank3CodeActivity::class.java).apply {
                                putExtra("payment_id", it)
                            })
                            finish()
                        }

                    } else {
                        val paymentUrl1 = it.data!!.pay_url
                        paymentUrl1?.let {
                            val intent = Intent(this, WebPayActivity::class.java)
                                .apply {
                                    putExtra("adInfo", it)
                                    putExtra("title", "支付")
                                }
                            startActivity(intent)
                            finish()
                        }
                    }
                }


            }
        })

    }

    fun ok() {
        if (viewModel.num.value == null || viewModel.txType.value!! < 0) {//
            toast("所有不得为空")
            return
        }

        when (viewModel.txType.value) {

            3 -> {
                PayDetailDialog(this).apply {
                    vm = viewModel
                    payMoney.value = "${viewModel.num.value}"
                    val a = this
                    onSelectBank = { money: String, bankNo: String, tokenNo: String ->
                        LogUtils.i("${money + " - " + bankNo + " - " + tokenNo}")
                        viewModel.ok3(tokenNo)
                        a.cancel()
                    }

                }.show()
            }

            5 -> {
                startActivity(Intent(this, DePayActivity::class.java).apply {
                    putExtra("num", viewModel.num.value)
                    putExtra("txType", viewModel.txType.value)
                })
            }

            else -> {
                viewModel.ok()

            }
        }

    }

    fun goRecord() {
        startActivity(Intent(this, RechargeOrderActivity::class.java))
//        startActivity(Intent(this, RechargeRecordActivity::class.java))

    }


}